﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KinSocket;

namespace KinConquer.Packets.Handles
{
    public class GeneralData
    {
        public unsafe static void Handle(Byte* Data, KinSocket.Connection Client)
        {
            Structures.DataPacket* Packet = (Structures.DataPacket*)Data;
            Entities.Hero Hero = Program.GetHero(Client.Identifier);

            switch (Packet->DataType)
            {
                case Structures.DataPacket.DataTypes.Jump:
                    {
                        if (Hero.DoneJumping)
                        {
                            UInt16 newX = (UInt16)Packet->dwParam;
                            UInt16 newY = (UInt16)(Packet->dwParam >> 16);

                            if (Entities.Calculations.Misc.Distance(Hero.X, Hero.Y, newX, newY) < Constants.JumpDistance)
                            {
                                Hero.Angle = Entities.Calculations.Misc.Angle(Hero.X, Hero.Y, newX, newY);
                                Hero.Action = 100;

                                Hero.Owner.Send(Packet);
                                Hero.EnqeueAction(new Entities.QdAction(Entities.QdActionType.SendRangePacket, Packet, true));
                                
                                Hero.X = newX;
                                Hero.Y = newY;

                                Hero.EnqeueAction(new Entities.QdAction(Entities.QdActionType.UpdateCleanScreen) { Bool = true });
                            }
                        }
                        Hero.PullBack(Hero.X, Hero.Y);
                        break;
                    }
                case Structures.DataPacket.DataTypes.InvisibleEntity:
                    {
                        Entities.Hero h = Program.GetHero(Packet->dwParam);
                        if (h != null)
                        {
                            Hero.Owner.Send(h.SpawnPacket);
                        }
                        break;
                    }
                case Structures.DataPacket.DataTypes.GetSurroundings:
                    {
                        Hero.EnqeueAction(new Entities.QdAction(Entities.QdActionType.GetSurroundings, Data, true));
                        break;
                    }
                case Structures.DataPacket.DataTypes.CompleteLogin:
                    {
                        Hero.EnqeueAction(new Entities.QdAction(Entities.QdActionType.SendRangePacket, Hero.SpawnPacket, true));
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.SetLocation:
                    {
                        Packet->dwParam = Hero.Map;
                        Packet->wParam1 = Hero.X;
                        Packet->wParam2 = Hero.Y;
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.ChangePKMode:
                    {
                        Hero.PKMode = (Entities.PKMode)Math.Min(Packet->dwParam, 3);
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.Hotkeys:
                    {
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.ConfirmFriends:
                    {
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.ConfirmGuild:
                    {
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.ConfirmProficiencies:
                    {
                        Client.Send(Packet);
                        break;
                    }
                case Structures.DataPacket.DataTypes.ConfirmSpells:
                    {
                        Client.Send(Packet);
                        break;
                    }
                default: Console.WriteLine("Unhandled GeneralData type: {0}", Packet->DataType); break;
            }
        }
    }
}
